package com.github.mhendred.face4j.examples;

import java.util.List;

import com.github.mhendred.face4j.DefaultFaceClient;
import com.github.mhendred.face4j.FaceClient;
import com.github.mhendred.face4j.exception.FaceClientException;
import com.github.mhendred.face4j.exception.FaceServerException;
import com.github.mhendred.face4j.model.Face;
import com.github.mhendred.face4j.model.Guess;
import com.github.mhendred.face4j.model.Photo;

/**
 * This is an example of using the client to train your index for one new user. For facebook
 * you can skip the training step and  just call recognize with facebook credentials set
 * 
 * @author mhendred
 *
 */
public class ClientExample 
{
	/**
	 * Your face.com API key
	 */
	protected static final String API_KEY = "0889a9494b9775f3fadd8ef7179ab671";
	
	/**
	 * Your face.com API secret
	 */
	protected static final String API_SEC = "4f670ea4195306cbc88a940d5d1b72d2";

	/**
	 * A url of a photo with faces in it
	 */
//	protected static final String URL_WITH_FACES = "http://seedmagazine.com/images/uploads/attractive_article.jpg";
	protected static final String URL_WITH_FACES = "https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash2/30614_1461706027194_1369743791_1272296_6375915_n.jpg";
	
	/**
	 * Your face.com API namespace
	 */
	protected static final String NAMESPACE = "fabio.melillo";

	/**
	 * user id to recognize
	 */
	protected static final String USER_ID = "a_user_id@" + NAMESPACE;
	
    public static void main(String[] args) throws FaceClientException, FaceServerException
    {
    	FaceClient faceClient = new DefaultFaceClient(API_KEY, API_SEC);
    	
    	/**
    	 * First we detect some faces in a url. This URL has a single face, So we get back one
    	 * Photo object with one Face object in it.
    	 * 
    	 * You can pass more than one URL (comma delimited String) or you can pass an image file    
    	 * 
    	 * @see http://developers.face.com/docs/api/faces-detect/
    	 * @see http://developers.face.com/docs/api/faces-recognize/
    	 */
    	Photo photo;
//    	 photo = faceClient.detect(URL_WITH_FACES).get(0);
//    
//    	/*
//    	 * Now we pull out the temporary tag and call save with the desired username and label.
//    	 */
//    	Face f = photo.getFace();
//    	faceClient.saveTags(f.getTID(), USER_ID, "a label");
//    	
//    	/*
//    	 * Let get the training status for this user now. We should see training in progress TRUE
//    	 * because we havent called train yet.
//    	 */
//    	faceClient.status(USER_ID);
//    	
//    
//    	/*
//    	 * IMPORTANT: Now we call train on our untrained user. This will commit our saved tag for this user to
//    	 * the database so we can recognize them later with 'recognize' calls   
//    	 */
//    	faceClient.train(USER_ID);
    	
    	/**
    	 * Now we can call recognize. Look for any user in our index (we only have one now)
    	 * We should see a guess now
    	 */
    	photo = faceClient.recognize(URL_WITH_FACES, "all@" + NAMESPACE).get(0);
    	System.out.println(photo.getFaceCount()+" Detected");
    	int i=0;
    	for (Face face : photo.getFaces())
    	{
    		if (face.getFaceConfidence()>50 && face.getGuesses().size()>0)
    		{	
    			Guess guess=face.getGuess();
    			String guessString=guess.toString();
    			String currLabel=guessString.substring(guessString.indexOf("=")+1, guessString.indexOf("@"));
    			face.setLabel(currLabel);
//    			System.out.println("Label: " + face.getLabel());
        		System.out.println(face.getGuess());
        		System.out.println("• "+face.getLabel());
        		i++;
    		}
    	}
    	System.out.println(i+" Suggested");
    	
    	List<String> utenti=faceClient.users(NAMESPACE).getUsers(NAMESPACE);
    	System.out.println("ID presenti nel namespace "+NAMESPACE);
    	for (String curr : utenti) {
			System.out.println(curr);
		}
    	
    	
    }
}